package org.cid54.bukkit.command;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.event.player.PlayerChatEvent;
import org.cid54.bukkit.Cid54PlayerListener;
import org.cid54.bukkit.command.util.PlayerCommand;

/**
 *
 * @author Cid54
 */
public class CilinderCommand implements PlayerCommand {

    @Override
    public boolean execute(String command, PlayerChatEvent event, Cid54PlayerListener listener) {

        String[] commands = command.split(" ");

        if (commands.length > 3) {

            char direction = commands[1].toLowerCase().charAt(0);

            int r = Integer.parseInt(commands[2]);

            int height = Integer.parseInt(commands[3]);

            Material mat = Material.getMaterial(Integer.parseInt(commands[4]));

            Location loc = Cid54PlayerListener.cloneLocation(event.getPlayer().getLocation());

            World w = event.getPlayer().getWorld();

            //x² + z² = r²

            if (direction == 'y') {

                for (int y = 0; y < height; y++) {

                    this.makeCilinderY(loc, w, mat, r);

                    loc.setY(loc.getY() + 1D);

                }
                return true;

            } else if (direction == 'x') {


                for (int x = 0; x < height; x++) {

                    this.makeCilinderX(loc, w, mat, r);

                    loc.setX(loc.getX() + 1D);

                }
                return true;

            } else if (direction == 'z') {

                for (int z = 0; z < height; z++) {

                    this.makeCilinderZ(loc, w, mat, r);

                    loc.setZ(loc.getZ() + 1D);

                }
                return true;
            } else {
                return false;
            }


        }

        return false;

    }

    private void makeCilinderX(Location loc, World w, Material mat, int r) {

        for (int y = -r; y <= r; y++) {

            Location l = Cid54PlayerListener.cloneLocation(loc);

            l.setY(l.getY() + y);

            double z = Math.sqrt(r * r - y * y);

            l.setZ(l.getZ() + z);
            w.getBlockAt(l).setType(mat);

            l.setZ(l.getZ() - 2.0D * z);
            w.getBlockAt(l).setType(mat);
        }

        for (int z = -r; z <= r; z++) {

            Location l = Cid54PlayerListener.cloneLocation(loc);

            l.setZ(l.getZ() + z);

            double y = Math.sqrt(r * r - z * z);

            l.setY(l.getY() + y);
            w.getBlockAt(l).setType(mat);

            l.setY(l.getY() - 2.0D * y);
            w.getBlockAt(l).setType(mat);
        }
    }

    private void makeCilinderZ(Location loc, World w, Material mat, int r) {

        for (int y = -r; y <= r; y++) {

            Location l = Cid54PlayerListener.cloneLocation(loc);

            l.setY(l.getY() + y);

            double x = Math.sqrt(r * r - y * y);

            l.setX(l.getX() + x);
            w.getBlockAt(l).setType(mat);

            l.setX(l.getX() - 2.0D * x);
            w.getBlockAt(l).setType(mat);
        }

        for (int x = -r; x <= r; x++) {

            Location l = Cid54PlayerListener.cloneLocation(loc);

            l.setX(l.getX() + x);

            double y = Math.sqrt(r * r - x * x);

            l.setY(l.getY() + y);
            w.getBlockAt(l).setType(mat);

            l.setY(l.getY() - 2.0D * y);
            w.getBlockAt(l).setType(mat);
        }
    }

    private void makeCilinderY(Location loc, World w, Material mat, int r) {

        for (int x = -r; x <= r; x++) {

            Location l = Cid54PlayerListener.cloneLocation(loc);

            l.setX(l.getX() + x);

            double z = Math.sqrt(r * r - x * x);

            l.setZ(l.getZ() + z);
            w.getBlockAt(l).setType(mat);

            l.setZ(l.getZ() - 2.0D * z);
            w.getBlockAt(l).setType(mat);
        }

        for (int z = -r; z <= r; z++) {

            Location l = Cid54PlayerListener.cloneLocation(loc);

            l.setZ(l.getZ() + z);

            double x = Math.sqrt(r * r - z * z);

            l.setX(l.getX() + x);
            w.getBlockAt(l).setType(mat);

            l.setX(l.getX() - 2.0D * x);
            w.getBlockAt(l).setType(mat);
        }
    }

    @Override
    public String getCommand() {
        return "cilinder";
    }

    @Override
    public String getHelpText() {
        return "Cilinder <x|y|z> <radius> <heigth> <material_id>. Creates a cilinder around you.";
    }
}
